package cn.gailvlun.gll.presentation.emotion.util

import android.text.InputFilter
import android.text.Spanned
import cn.gailvlun.gll.util.ToastUtil


class LengthFilter(var max: Int) : InputFilter {

    override fun filter(source: CharSequence, start: Int, end: Int, dest: Spanned, dstart: Int, dend: Int): CharSequence? {


        val textLength = (dest.length - (dend - dstart))
        var keep = max - textLength

        when {
            keep <= 0 -> {
                ToastUtil.showShort("最多输入4000个字符")
                return ""
            }
            keep >= end - start -> return null 
            else -> {
                keep += start
                if (Character.isHighSurrogate(source[keep - 1])) {
                    --keep
                    if (keep == start) {
                        return ""
                    }
                }
                return source.subSequence(start, keep)
            }
        }
    }
}